﻿using System.Web;


namespace KamLib.Web.MVC
{


    public class MvcText : IHtmlString
    {
        public string Attrs;
        public string Class;
        public string Style;
        public string Value { get; set; }

        public MvcText() { }
        public MvcText(string value)
        {
            Value = value;
        }


        public override string ToString()
        {
            return ToHtmlString();
        }


        public string ToHtmlString()
        {
            return Value;
        }


        #region Operators

        public static implicit operator MvcText(string value)
        {
            return new MvcText(value);
        }

        public static implicit operator string(MvcText value)
        {
            return value != null ? value.Value : null;
        }


        public static MvcText operator +(MvcText a, MvcText b)
        {
            if (a == null) return b;
            if (b == null) return a;

            var s = new MvcText(a.Value + b.Value);
            s.Attrs(a.Attrs).Attrs(b.Attrs);
            s.Class(a.Class).Class(b.Class);
            s.Style(a.Style).Style(b.Style);

            return s;
        }

        public static MvcText operator +(string a, MvcText b)
        {
            return (MvcText)a + b;
        }

        public static MvcText operator +(MvcText a, string b)
        {
            return a + (MvcText)b;
        }

        #endregion


    }


    public static class MvcTextHelper
    {

        public static bool yes(this MvcText me) { return me != null && me.Value.yes(); }
        public static bool no(this MvcText me) { return me == null || me.Value.no(); }


        public static MvcText Attrs(this MvcText me, string value)
        {
            if (me == null)
                return new MvcText { Attrs = value };

            if (me.Attrs.yes())
                me.Attrs += " " + value;
            else
                me.Attrs = value;
            return me;
        }

        public static MvcText Class(this MvcText me, string value)
        {
            if (me == null)
                return new MvcText { Class = value };

            if (me.Class.yes())
                me.Class += " " + value;
            else
                me.Class = value;
            return me;
        }

        public static MvcText Style(this MvcText me, string value)
        {
            if (me == null)
                return new MvcText { Style = value };
            me.Style += value + ";";
            return me;
        }


        public static MvcText Attrs(this string me, string value)
        {
            return Attrs((MvcText)me, value);
        }

        public static MvcText Class(this string me, string value)
        {
            return Class((MvcText)me, value);
        }

        public static MvcText Style(this string me, string value)
        {
            return Style((MvcText)me, value);
        }


    }

}
